package com.example.cinema.activity;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.databinding.library.baseAdapters.BR;

import com.example.cinema.CountListHeight;
import com.example.cinema.R;
import com.example.cinema.adapter.ReplyListAdapter;
import com.example.cinema.bindingdata.CircleItemData;
import com.example.cinema.bindingdata.UserCircleData;
import com.example.cinema.databinding.ActivityCircleBinding;
import com.example.cinema.drawview.CircleVideoPlayer;
import com.example.cinema.testdara.TestData;

import java.util.List;

//贴子详情
public class ActivityCircle extends AppCompatActivity {

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK)
        {
            if(playheigh>0)
            {
                Suo();
            }

        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {

        ACB.videoplayer.startplay();
        super.onResume();
    }

    @Override
    protected void onPause() {

        ACB.videoplayer.pauseplay();
        super.onPause();
    }

    @Override
    protected void onStop() {

        ACB.videoplayer.pauseplay();
        super.onStop();
    }

    @Override
    protected void onDestroy() {

        ACB.videoplayer.stopplay();
        super.onDestroy();
    }


    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        ACB.videoplayer.loadVideo();
    }

    private ActivityCircleBinding ACB;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ACB = DataBindingUtil.setContentView(this, R.layout.activity_circle);
        init();
        if(Build.VERSION.SDK_INT>11 && Build.VERSION.SDK_INT<19)
        {
            View view = getWindow().getDecorView();
            view.setSystemUiVisibility(View.GONE);
        }else if(Build.VERSION.SDK_INT>=19){
            View view = getWindow().getDecorView();
            int UIOP = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY|View.SYSTEM_UI_FLAG_FULLSCREEN;
            view.setSystemUiVisibility(UIOP);
        }
    }

    Handler  handler = new Handler(Looper.myLooper())
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            Log.i("AAA","执行测量");
        }
    };


    private int playheigh = 0;
    private void init() {

        //帖子详情
        List<UserCircleData> data = TestData.TestUserCircke();
        ACB.setData(data.get(0));

        //评论数据
        List<CircleItemData> list = TestData.TestCircleItem();
        for (int i=0;i<list.size();i++)
        {
            ViewDataBinding dataBinding = DataBindingUtil.inflate(LayoutInflater.from(getApplicationContext()),R.layout.circle_item,null,false);
            dataBinding.setVariable(BR.circledata,list.get(i));
            View zan = dataBinding.getRoot().findViewById(R.id.zan);
            zan.setOnClickListener(new ZanClick());//点赞
            ACB.list.addView(dataBinding.getRoot());
        }
        //圈主点赞
        ACB.zan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("PPPP","点赞圈主");
            }
        });
        //返回
        ACB.finsh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //全屏点击回调
        ACB.videoplayer.setOnFullClickListener(new CircleVideoPlayer.OnFullClickListener() {
            @Override
            public void fullClickState(int state) {
                if(playheigh==0)
                {
                    playheigh = ACB.videoplayer.getMeasuredHeight();
                }
                switch (state)
                {
                    case 1:
                        Full();
                        break;
                    case 2:
                        Suo();
                        break;
                }
            }
        });
    }

    public void Full() {
        hidevs();
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
        {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
            ACB.videoplayer.allquan();
            ACB.topname.setVisibility(View.GONE);
            ACB.sendtext.setVisibility(View.GONE);
        }

    }

    public void Suo()
    {
        if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
        {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
            ACB.videoplayer.narrow(playheigh);
            Log.i("TTTTT",playheigh+"");
            ACB.topname.setVisibility(View.VISIBLE);
            ACB.sendtext.setVisibility(View.VISIBLE);
        }

    }

    //评论点赞
    class ZanClick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            Log.i("PPPP","评论点赞");
        }
    }


    //隐藏状态栏以及虚拟按键
    private void hidevs()
    {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        if(Build.VERSION.SDK_INT>11 && Build.VERSION.SDK_INT<19)
        {
            View view = getWindow().getDecorView();
            view.setSystemUiVisibility(View.GONE);
        }else if(Build.VERSION.SDK_INT > 19)
        {
            View view = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            view.setSystemUiVisibility(uiOptions);
        }
    }

}
